/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjs.liyq.web;

import edu.fjs.liyq.entity.User;
import edu.fjs.liyq.exception.AccountNotFoundException;
import edu.fjs.liyq.exception.IncorrectPasswordException;
import edu.fjs.liyq.service.UserService;
import edu.fjs.liyq.service.impl.UserServiceImpl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author danny
 */
public class AjaxLoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         // 获取客户端的输入
        String userName = request.getParameter("userName");
        String password = request.getParameter("pwd");
        System.out.println("用户名：" + userName + "\t密码：" + password);
        
        // 调用业务逻辑
        UserService service = new UserServiceImpl();
        // 封装user对象
        User user = new User();
        user.setAccount(userName);
        user.setPassword(password);
        
        User userFromDB = null;
        try {
            userFromDB = service.authenticate(user);
        } catch (AccountNotFoundException e) {
            out.print("无此用户！");
            out.flush();
            out.close();
            return;
        } catch (IncorrectPasswordException e) {
            out.print("密码错误！");
            out.flush();
            out.close();
            return;
        }
        
        if (userFromDB != null) {
            // 存cookie
            Cookie ckUserName = new Cookie("userName", userName);
            Cookie ckPassword = new Cookie("password", password);
            ckUserName.setMaxAge(120);
            ckPassword.setMaxAge(120);
            response.addCookie(ckUserName);
            response.addCookie(ckPassword);
            
            // 将登录成功的用户信息保存到session
            HttpSession session = request.getSession();
//            session.setAttribute("LOGIN_USER", userName);
            session.setAttribute("LOGIN_USER", userFromDB);
            
            out.print("success");
            out.flush();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
